<template>
  <div v-loading="loading" class="container">

    <el-card shadow="always">

      <el-form ref="form" :inline="true" label-position="left" size="mini">

        <el-form-item label="主机:" style="margin-bottom: 0">
          <el-select
            v-model="hostSelectCondition"
            style="min-width: 130px;"
            clearable
            filterable
            placeholder="请选择主机"
          >
            <el-option
              v-for="item in selectData"
              :key="item.id"
              :label="item.host"
              :value="item.host"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="标签:" style="margin-bottom: 0">

          <el-select
            v-model="tagsSelectCondition"
            style="min-width: 130px;"
            clearable
            filterable
            placeholder="请选择标签"
          >
            <el-option
              v-for="item in selectData"
              :key="item.id"
              :label="item.tags"
              :value="item.tags"
            />
          </el-select>
        </el-form-item>

        <el-form-item style="margin-bottom: 0">
          <el-button type="primary" @click="create()">新建</el-button>
        </el-form-item>

      </el-form>

      <el-divider />

      <el-table
        :data="tableData"
        border
        header-align="center"
        style="min-width: 300px; margin-bottom: 20px"
        size="mini"
      >
        <el-table-column label="服务器" align="center">
          <el-table-column prop="id" fixed="left" min-width="60" label="ID" align="center" />
          <el-table-column prop="host" min-width="110" label="主机" align="center" />
          <el-table-column prop="port" min-width="60" label="端口" align="center" />
          <el-table-column prop="tags" min-width="120" label="标签" align="center" />
        </el-table-column>

        <el-table-column label="监控开关" align="center">
          <el-table-column prop="monitor" min-width="90" label="监控" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.monitor"
                active-color="#13ce66"
                :active-value="1"
                inactive-color="#ff4949"
                :active-inactive-value="0"
                disabled
              />
            </template>
          </el-table-column>
          <el-table-column prop="send_mail" min-width="90" label="发送邮件" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.send_mail"
                active-color="#13ce66"
                :active-value="1"
                inactive-color="#ff4949"
                :active-inactive-value="0"
                disabled
              />
            </template>
          </el-table-column>
          <el-table-column prop="send_sms" min-width="90" label="发送短信" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.send_sms"
                active-color="#13ce66"
                :active-value="1"
                inactive-color="#ff4949"
                :active-inactive-value="0"
                disabled
              />
            </template>
          </el-table-column>
          <el-table-column prop="slow_query" min-width="90" label="慢查询" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.slow_query"
                active-color="#13ce66"
                :active-value="1"
                inactive-color="#ff4949"
                :active-inactive-value="0"
                disabled
              />
            </template>
          </el-table-column>
        </el-table-column>

        <el-table-column label="告警项目" align="center">
          <el-table-column prop="alarm_threads_connected" min-width="90" label="连接线程数" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.alarm_threads_connected"
                active-color="#13ce66"
                :active-value="1"
                inactive-color="#ff4949"
                :active-inactive-value="0"
                disabled
              />
            </template>
          </el-table-column>
          <el-table-column prop="alarm_threads_running" min-width="90" label="活动线程数" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.alarm_threads_running"
                active-color="#13ce66"
                :active-value="1"
                inactive-color="#ff4949"
                :active-inactive-value="0"
                disabled
              />
            </template>
          </el-table-column>
          <el-table-column prop="alarm_threads_waits" min-width="90" label="等待线程数" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.alarm_threads_waits"
                active-color="#13ce66"
                :active-value="1"
                inactive-color="#ff4949"
                :active-inactive-value="0"
                disabled
              />
            </template>
          </el-table-column>
        </el-table-column>

        <el-table-column label="操作" align="center" fixed="right" min-width="160">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="modifyItem(scope.row)">编辑
            </el-button>
            <el-button size="mini" type="danger" @click="removeItem(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        background=""
        layout="prev, pager, next"
        :current-page="pageIndex"
        :page-size="pageSize"
        :total="totalItems"
        @current-change="curPageChange"
      />
    </el-card>

    <el-dialog :visible.sync="dialogVisible" width="800px" :close-on-click-modal="false">

      <el-form ref="editInfoForm" :model="editInfo" label-width="100px">
        <el-form-item
          label="主机:"
          prop="host"
          :rules="[{required: true, message: '请填写主机', trigger:
            ['blur', 'change']}]"
        >
          <el-input
            v-model="editInfo.host"
            :disabled="!isCreatePgsqlServer"
            size="mini"
            style="width: 200px"
            placeholder="请输入主机"
          />
        </el-form-item>
        <el-form-item
          label="端口:"
          prop="port"
          :rules="[{required: true, message: '请填写端口', trigger:
            ['blur', 'change']}]"
        >
          <el-input v-model="editInfo.port" size="mini" style="width: 200px" placeholder="请输入端口" />
        </el-form-item>
        <el-form-item label="用户名:" prop="username" :rules="[{required: true, message: '请填写用户名', trigger:['blur', 'change']}]">
          <el-input v-model="editInfo.username" size="mini" style="width: 200px" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item v-if="isCreatePgsqlServer" label="密码:" prop="password" :rules="[{required: true, message: '请填写密码', trigger:['blur', 'change']}]">
          <el-input v-model="editInfo.password" size="mini" style="width: 200px" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="标签:" prop="tags" :rules="[{required: true, message: '请填写标签', trigger:['blur', 'change']}]">
          <el-input v-model="editInfo.tags" size="mini" style="width: 200px" placeholder="请输入标签" />
        </el-form-item>
        <el-divider />

        <el-form-item label="监控:" prop="monitor">
          <el-switch v-model="editInfo.monitor" :active-value="1" active-color="#13ce66" :inactive-value="0" inactive-color="#ff4949" />
        </el-form-item>

        <div v-if="editInfo.monitor">

          <el-row>
            <el-col :span="4">
              <el-form-item label="发送邮件:" prop="send_mail">
                <el-switch v-model="editInfo.send_mail" :active-value="1" active-color="#13ce66" :inactive-value="0" inactive-color="#ff4949" />
              </el-form-item>
            </el-col>
            <el-col v-if="editInfo.send_mail" :push="1" :span="10">
              <el-form-item
                label-width="130px"
                label="告警邮件收件人:"
                prop="send_mail_to_list"
                :rules="[{required: true, message: '请填写告警邮件收件人',
                          trigger:['blur', 'change']}]"
              >
                <el-input v-model="editInfo.send_mail_to_list" size="mini" style="width: 300px" placeholder="多人请用,分割" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="4">
              <el-form-item label="发送短信:" prop="send_sms">
                <el-switch v-model="editInfo.send_sms" :active-value="1" active-color="#13ce66" :inactive-value="0" inactive-color="#ff4949" />
              </el-form-item>
            </el-col>
            <el-col v-if="editInfo.send_sms" :push="1" :span="10">
              <el-form-item
                label-width="130px"
                label="告警短信收件人:"
                prop="send_sms_to_list"
                :rules="[{required: true, message: '请填写告警短信收件人',
                          trigger:['blur', 'change']}]"
              >
                <el-input v-model="editInfo.send_sms_to_list" size="mini" style="width: 300px" placeholder="多人请用,分割" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="4">
              <el-form-item label="慢查询:" prop="slow_query">
                <el-switch v-model="editInfo.slow_query" :active-value="1" active-color="#13ce66" :inactive-value="0" inactive-color="#ff4949" />
              </el-form-item>
            </el-col>
            <el-col v-if="editInfo.slow_query" :push="1" :span="10">
              <el-form-item
                label-width="130px"
                label="慢查询推送邮箱:"
                prop="send_slowquery_to_list"
                :rules="[{required: true, message: '请填写慢查询推送邮箱',
                          trigger:['blur', 'change']}]"
              >
                <el-input v-model="editInfo.send_slowquery_to_list" size="mini" style="width: 300px" placeholder="多人请用,分割" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="4">
              <el-form-item label="连接线程数:" prop="alarm_threads_connected">
                <el-switch v-model="editInfo.alarm_threads_connected" :active-value="1" active-color="#13ce66" :inactive-value="0" inactive-color="#ff4949" />
              </el-form-item>
            </el-col>
            <el-col v-if="editInfo.alarm_threads_connected" :push="1" :span="8">
              <el-form-item
                label-width="90px"
                label="警告阈值:"
                prop="threshold_warning_threads_connected"
                :rules="[{required: true, message:
                            '请填写连接线程数警告阈值',
                          trigger:['blur', 'change']}]"
              >
                <el-input-number v-model="editInfo.threshold_warning_threads_connected" :precision="0" controls-position="right" :min="1" size="small" />
              </el-form-item>
            </el-col>
            <el-col v-if="editInfo.alarm_threads_connected" :push="1" :span="8">
              <el-form-item
                label-width="90px"
                label="紧急阈值:"
                prop="threshold_critical_threads_connected"
                :rules="[{required: true, message:
                            '请填写连接线程数紧急阈值',
                          trigger:['blur', 'change']}]"
              >
                <el-input-number v-model="editInfo.threshold_critical_threads_connected" :precision="0" controls-position="right" :min="1" size="small" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="4">
              <el-form-item label="活动线程数:" prop="alarm_threads_running">
                <el-switch v-model="editInfo.alarm_threads_running" :active-value="1" active-color="#13ce66" :inactive-value="0" inactive-color="#ff4949" />
              </el-form-item>
            </el-col>
            <el-col v-if="editInfo.alarm_threads_running" :push="1" :span="8">
              <el-form-item
                label-width="90px"
                label="警告阈值:"
                prop="threshold_warning_threads_running"
                :rules="[{required: true, message:
                            '请填写活动线程数警告阈值',
                          trigger:['blur', 'change']}]"
              >
                <el-input-number v-model="editInfo.threshold_warning_threads_running" :precision="0" controls-position="right" :min="1" size="small" />
              </el-form-item>
            </el-col>
            <el-col v-if="editInfo.alarm_threads_running" :push="1" :span="8">
              <el-form-item
                label-width="90px"
                label="紧急阈值:"
                prop="threshold_critical_threads_running"
                :rules="[{required: true, message:
                            '请填写活动线程数紧急阈值',
                          trigger:['blur', 'change']}]"
              >
                <el-input-number v-model="editInfo.threshold_critical_threads_running" :precision="0" controls-position="right" :min="1" size="small" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="4">
              <el-form-item label="等待线程数:" prop="alarm_threads_waits">
                <el-switch v-model="editInfo.alarm_threads_waits" :active-value="1" active-color="#13ce66" :inactive-value="0" inactive-color="#ff4949" />
              </el-form-item>
            </el-col>
            <el-col v-if="editInfo.alarm_threads_waits" :push="1" :span="8">
              <el-form-item
                label-width="90px"
                label="警告阈值:"
                prop="threshold_warning_threads_waits"
                :rules="[{required: true, message:
                            '请填写等待线程数警告阈值',
                          trigger:['blur', 'change']}]"
              >
                <el-input-number v-model="editInfo.threshold_warning_threads_waits" :precision="0" controls-position="right" :min="1" size="small" />
              </el-form-item>
            </el-col>
            <el-col v-if="editInfo.alarm_threads_waits" :push="1" :span="8">
              <el-form-item
                label-width="90px"
                label="紧急阈值:"
                prop="threshold_critical_threads_waits"
                :rules="[{required: true, message:
                            '请填写等待线程数紧急阈值',
                          trigger:['blur', 'change']}]"
              >
                <el-input-number v-model="editInfo.threshold_critical_threads_waits" :precision="0" controls-position="right" :min="1" size="small" />
              </el-form-item>
            </el-col>
          </el-row>

        </div>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>

import { removePgsqlServer, addPgsqlServer, modifyPgsqlServer, pgsqlServersSimpleInfo, pgsqlServers } from '../../api/api'

export default {
  data() {
    return {
      loading: false,
      selectData: [],
      hostSelectCondition: '',
      tagsSelectCondition: '',
      tableData: [],
      editInfo: {},
      isCreatePgsqlServer: false,
      dialogVisible: false,
      editInfoBackup: {
        host: '',
        port: '',
        username: '',
        password: '',
        tags: '',
        monitor: false,
        send_mail: false,
        send_mail_to_list: '',
        send_sms: false,
        send_sms_to_list: '',
        slow_query: false,
        send_slowquery_to_list: '',
        alarm_threads_connected: false,
        threshold_warning_threads_connected: 1000,
        threshold_critical_threads_connected: 3000,
        alarm_threads_running: false,
        threshold_warning_threads_running: 10,
        threshold_critical_threads_running: 30,
        alarm_threads_waits: false,
        threshold_warning_threads_waits: 5,
        threshold_critical_threads_waits: 15
      },
      pageSize: 15,
      pageIndex: 1,
      totalItems: 0,
      statisticsDialogVisible: false,
      statisticsDialogLoading: false
    }
  },
  watch: {
    hostSelectCondition: function(val) {
      this.pageIndex = 1
      this.getPgsqlServers()
    },
    tagsSelectCondition: function(val) {
      this.pageIndex = 1
      this.getPgsqlServers()
    }
  },
  mounted() {
    this.getPgsqlServersSimpleInfo()
    this.getPgsqlServers()
  },
  destroyed() {
  },
  methods: {
    create() {
      this.isCreatePgsqlServer = true
      this.dialogVisible = true
      this.$nextTick(() => {
        this.editInfo = JSON.parse(JSON.stringify(this.editInfoBackup))
        this.$refs.editInfoForm.resetFields()
      })
    },
    modifyItem(item) {
      this.isCreatePgsqlServer = false
      this.dialogVisible = true
      this.$nextTick(() => {
        this.editInfo = JSON.parse(JSON.stringify(item))
        this.$refs.editInfoForm.resetFields()
      })
    },
    removeItem(item) {
      this.$confirm('此操作将删除该节点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.removePgsqlServer(item.id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    submit() {
      this.$refs.editInfoForm.validate((valid) => {
        if (valid) {
          if (!this.isCreatePgsqlServer) {
            this.modifyPgsqlServer()
          } else {
            this.addPgsqlServer()
          }
        } else {
          console.log('=========', this.editingInfo)
          console.log('error submit!!')
          return false
        }
      })
    },
    removePgsqlServer(id) {
      const _this = this
      const loading = this.$loading({
        lock: true,
        text: '正在删除，请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      removePgsqlServer({ server_id: id }).then((res) => {
        loading.close()
        _this.$message({
          message: '删除成功',
          type: 'success'
        })
        _this.getPgsqlServers()
        _this.getPgsqlServersSimpleInfo()
      }).catch(() => {
        loading.close()
      })
    },
    addPgsqlServer() {
      const _this = this
      const loading = this.$loading({
        lock: true,
        text: '正在添加，请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      addPgsqlServer(this.editInfo).then((res) => {
        loading.close()
        _this.dialogVisible = false
        _this.$message({
          message: '添加成功',
          type: 'success'
        })
        _this.getPgsqlServers()
        _this.getPgsqlServersSimpleInfo()
      }).catch(() => {
        loading.close()
      })
    },
    modifyPgsqlServer() {
      const _this = this
      const loading = this.$loading({
        lock: true,
        text: '正在添加，请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      modifyPgsqlServer(this.editInfo).then((res) => {
        loading.close()
        _this.dialogVisible = false
        _this.$message({
          message: '修改成功',
          type: 'success'
        })
        _this.getPgsqlServers()
        _this.getPgsqlServersSimpleInfo()
      }).catch(() => {
        loading.close()
      })
    },
    getPgsqlServersSimpleInfo() {
      pgsqlServersSimpleInfo({}).then((res) => {
        this.selectData = res.data
      }).catch(() => {
      })
    },
    getPgsqlServers() {
      this.loading = true
      pgsqlServers({ host: this.hostSelectCondition, tags: this.tagsSelectCondition }).then((res) => {
        this.loading = false
        this.tableData = res.data
      }).catch(() => {
        this.loading = false
      })
    },
    async curPageChange(page) {
      this.pageIndex = page
    }
  }
}

</script>

<style lang="scss" scoped>

    @import "../../common/vue-hljs-theme.css";

    .container {
        display: flex;
        flex-direction: column;
        width: 100%;
    }

    .el-divider--horizontal {
        margin: 20px 0;
    }

    .el-col-5 {
        width: 20%;
    }

    .el-drawer:focus {
        outline: none;
    }

</style>

